<?php
/**
 * Created by PhpStorm.
 * User: xujiabin
 * Date: 2017/1/16
 * Time: 下午3:57
 */
namespace app\controllers;

use app\models\Find;
use app\models\Find_hint;
use app\models\Get;
use app\models\Get_hint;
use app\models\Users;
use phpDocumentor\Reflection\Types\Array_;
use yii\base\Controller;
use yii\db\Query;


/**get 是指捡到了；Find 是指要找**/
class AjaxController extends Controller
{
    /** ajax登陆接口传入username password 返回登陆成功为1 否则返回为0  **/
    public function actionSignin()
    {
        if (!\Yii::$app->request->isAjax){
            return 0;
        }else{
            $username = \Yii::$app->request->post('username');
            $password = \Yii::$app->request->post('password');
            $result = Users::find()->where(['username'=> $username,'password'=>md5($password)])->all();
            if ($result){
                return 1;
            }else{
                return 0;
            }
        }
    }

    /**ajax注册接口 传入username password email 如果注册成功返回1 否则返回0**/
    public function actionSignup()
    {
        if (!\Yii::$app->request->isAjax){
            return 0;
        }else{
            $username = \Yii::$app->request->post('username');
            $password = \Yii::$app->request->post('password');
            $email = \Yii::$app->request->post('email');
            $result = Users::find()->where(['username'=>$username,'email'=>$email])->all();
            if ($result){
                return 0;
            }else{
                $model = new Users();
                $model->username = $username;
                $model->password = md5($password);
                $model->email = $email;
                $res = $model->save();
                if ($res){
                    return 1;
                }else{
                    return 0;
                }
            }
        }
    }

    /**ajax返回所有find数据接口**/
    public function actionFind()
    {
        if (!\Yii::$app->request->isAjax){
            return 0;
        }else{
            $result = Find::find()->where(['able'=>'0'])->all();
            foreach ($result as $b => $c){
                $result["$b"] = $c->ToArray();
            }
            foreach ($result as $d => &$e){
                foreach ($e as $f => &$g){
                    $g = urlencode($g);
                }
            }
//            var_dump($result);
            return json_encode($result);
        }
    }

    /**ajax返回所有get数据接口**/
    public function actionGet()
    {
        if (!\Yii::$app->request->isAjax){
            return 0;
        }else{
            $result = Get::find()->where(['able'=>'0'])->all();
            foreach ($result as $b => $c){
                $result["$b"] = $c->ToArray();
            }
            foreach ($result as $d => &$e){
                foreach ($e as $f => &$g){
                    $g = urlencode($g);
                }
            }
            return json_encode($result);
        }
    }

    /**ajax 传进id 返回物品的详细信息 type表示是get还是find**/
    public function actionDetails()
    {
        if (!\Yii::$app->request->isAjax){
            return 0;
        }else{
            $id = \Yii::$app->request->post('id');
            $type = \Yii::$app->request->post('type');
            if ($type == 'find') {
                $result = Find::find()->where(['id' => $id])->one();
            }else{
                $result = Get::find()->where(['id'=>$id])->one();
            }
            if ($result){
                $result = $result->ToArray();
                foreach ($result as $i => &$v){
                    $v = urlencode($v);
                }
                return json_encode($result);
            }else{
                return 0;
            }
        }
    }

    /**Ajax 传入物品详细信息，保存到数据库返回1 否则返回0 type来区分是get还是find**/
    public function actionThings()
    {
        if (!\Yii::$app->request->isAjax){
            return 0;
        }else{
            $type = \Yii::$app->request->post('type');
            $name = \Yii::$app->request->post('name');
            $class = \Yii::$app->request->post('class');
            $time = \Yii::$app->request->post('time');
            $place = \Yii::$app->request->post('place');
            $xueyuan = \Yii::$app->request->post('xueyuan');
            $xuanshang = \Yii::$app->request->post('xuanshang');
            $person = \Yii::$app->request->post('person');
            $tel = \Yii::$app->request->post('tel');
            $qq = \Yii::$app->request->post('qq');
            $more = \Yii::$app->request->post('more');
            $user_id = \Yii::$app->request->post('user_id');
            if ($type == 'find'){
                $model = new Find();
            }else{
                $model = new Get();
            }
            $model->name = $name;
            $model->time = $time;
            $model->class = $class;
            $model->place = $place;
            $model->xueyuan = $xueyuan;
            $model->xuanshang = $xuanshang;
            $model->person = $person;
            $model->tel = $tel;
            $model->qq = $qq;
            $model->more = $more;
            $model->users_id = $user_id;
            $res = $model->save();
            if ($res){
                return 0;
            }else{
                return 1;
            }

        }
    }

    /**Ajax传入type和id，返回数据所有留言信息**/
    public function actionAllmes()
    {
        if (\Yii::$app->request->isAjax){
            return 0;
        }else{
            $type = \Yii::$app->request->get('type');
            $id = \Yii::$app->request->get('id');
            if ($type == 'find'){
                $result = Find_hint::find()->where(['find_id1'=>$id])->all();
            }else{
                $result = Get_hint::find()->where(['get_id1'=>$id])->all();
            }
            foreach ($result as $i => &$v){
                $v = $v->ToArray();
                foreach ($v as $k => &$m){
                    $m = urlencode($m);
                }
            }
//            var_dump($result);
            return json_encode($result);
        }
    }

    /**Ajax 传入type和物品id，接受内容，时间 存到数据库返回1，否则返回0**/
    public function actionMessage()
    {
        if (!\Yii::$app->request->isAjax){
            return 0;
        }else{
            $type = \Yii::$app->request->get('type');
            if ($type == 'find'){
                $model = new Find_hint();
                $model->find_id1 = \Yii::$app->request->post('id');
            }else{
                $model = new Get_hint();
                $model->get_id1 = \Yii::$app->request->post('id');
            }
            $model->content = \Yii::$app->request->post('content');
            $model->time = \Yii::$app->request->post('time');
            $res = $model->save();
            if ($res){
                return 1;
            }else{
                return 0;
            }
        }
    }

    /**Ajax搜索接口**/
    public function actionSearch()
    {
        if (!\Yii::$app->request->isAjax){
            return 0;
        }else{
//            var_dump(\Yii::$app->request->get());
            $key = \Yii::$app->request->post('key');
            $place = \Yii::$app->request->post('place');
            $time = \Yii::$app->request->post('time');
            $class = \Yii::$app->request->post('class');
            $time2 = \Yii::$app->request->post('time2');


            $result1 = Find::find()->where("`able`=:able and `class` like :class and `place` like :place and `more` like :more and `time` > :time1 and `time` < :time2")
                ->addParams([':able'=>0,':class'=>'%'.$class.'%',':place'=>'%'.$place.'%',':more'=>'%'.$key.'%',':time1'=>(int)$time,':time2'=>(int)$time2])
                ->all();
            foreach ($result1 as $k => &$v){
                $v = $v->ToArray();
                foreach ($v as $m => &$n){
                    $n = urlencode($n);
                }
            }

            $result2 = Get::find()->where("`able`=:able and `class` like :class and `place` like :place and `more` like :more and `time` > :time1 and `time` < :time2")
                ->addParams([':able'=>0,':class'=>'%'.$class.'%',':place'=>'%'.$place.'%',':more'=>'%'.$key.'%',':time1'=>(int)$time,':time2'=>(int)$time2])
                ->all();
            foreach ($result2 as $k => &$v){
                $v = $v->ToArray();
                foreach ($v as $m=> &$n){
                    $n = urlencode($n);
                }
            }
            $result = Array();
            $result = array_merge($result1,$result2);
            return json_encode($result);



        }
    }

    /*ajax 加载更多留言*/
    public function actionMes_more(){
        $request=\Yii::$app->request;
        if ($request->isAjax){
            $id=$request->get("id");
            if ($request->isPost){
                $cur_num=intval($request->post("cur_num"));
                $load_num=intval($request->post("load_num"));
                $temp=$request->post("lost_find");
                $table=($temp == 1)? "find_hint": "get_hint";
                $get_id1=($temp==1)?"find_id1":"get_id1";

                $load_mes=(new Query())
                        ->select(["time","content"])
                        ->from($table)
                        ->where([$get_id1=>$id])
                        /*->offset($cur_num)
                        ->limit($load_num)*/
                        ->all();

                return json_encode($load_mes);

            }
        }
    }

}